ppackage com.itheima;

import java.util.Comparator;
import java.util.TreeSet;

public class TreeSetDemo {

    public static void main(String[] args) {
        int result=sum(1,2,3,4,5);
        System.out.println(result);
        //创建TreeSet集合对象
        test1();
        test2();}
    private static void test0() {
        TreeSet<Integer> set1 = new TreeSet<>();

        set1.add(5);
        set1.add(2);

        set1.add(1);
        set1.add(4);
        for (Integer integer : set1){
            System.out.println(integer);
        }
        //System.out.println(set1);
    }
    public static void test1() {
        TreeSet<Student> set = new TreeSet<>();
        set.add(new Student("LUO",19));
        set.add(new Student("ZHANG",18));
        set.add(new Student("WANG",20));
        for (Student s : set){
            System.out.println(s.getName() + "---" + s.getAge());
        }
    }
    public static void test2() {
        /*Comparator<Student> comparator = new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {

                return o1.getAge() - o2.getAge();
            }
        };*/
        Comparator<Student> comparator=  (o1, o2) -> o1.getAge() - o2.getAge();
        TreeSet <Student> set = new TreeSet<>(comparator);
        set.add(new Student("LUO",19));
        set.add(new Student("ZHANG",18));
        set.add(new Student("WANG",20));
        for (Student s : set){
            System.out.println(s.getName() + "---" + s.getAge());
        }
    }
    public static int sum(int...a){
        int total=0;
        for (int i=0;i<a.length;i++){
            total+=a[i];
        }
        return total;
    }
}
